<?php
class ehttp{
    
    public $ch;
    public $method = "GET";
    public $url = "";
    public $headers_arr = array();
    public $status_code = "0";

    function open($method,$url){
        $this->method = $method;
        $this->ch = curl_init();
        curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST,$method);
        curl_setopt($this->ch ,CURLOPT_URL,$url);
        curl_setopt($this->ch ,CURLOPT_HEADER,1);
        curl_setopt($this->ch ,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($this->ch,CURLOPT_TIMEOUT,30);
    }


    function setHeader($key,$val){
        $this->headers_arr[$key] = $val;
    }

    function setCookie($cookie){
        curl_setopt($this->ch,CURLOPT_COOKIE,$cookie);
    }

    function setUserAgent($useragent){
        $this->headers_arr["user-agent"] = $useragent;
    }

    //设置类型
    function setContentType($type){
        $this->headers_arr["Content-Type"] = $type;
    }

    function getHeaders(){
        $headers = [];
        foreach ($this->headers_arr as $key => $val){
            $headers[] = $key . ": " . $val;
        }
        return $headers;
    }

    //设定代理
    function setProxy($proxyIp){
        $arr = explode(":",$proxyIp);
        curl_setopt($this->ch, CURLOPT_PROXY,$arr[0]);
        curl_setopt($this->ch, CURLOPT_PROXYPORT, $arr[1]);
        curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    }

    //提交请求
    function send($data = null){
        $this->setHeader("Accept", "*/*");
        $this->setHeader("Accept-Language", "zh-cn");
        curl_setopt($this->ch,CURLOPT_HTTPHEADER,$this->getHeaders());
        if ($ssl){
            curl_setopt($this->ch,CURLOPT_SSL_VERIFYHOST,2);
            curl_setopt($this->ch,CURLOPT_SSL_VERIFYPEER,FLASE);
		}
        if($this->method == "POST"){
            curl_setopt($this->ch,CURLOPT_POSTFIELDS,$data);
		    curl_setopt($this->ch,CURLOPT_POST,1);
        }
        curl_setopt($this->ch,CURLOPT_COOKIESESSION,true);
        $result = curl_exec($this->ch);
        $this->status_code = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
        $headerSize = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
        $body = substr($result, $headerSize);
        $this->ret_headeres = substr($result,0,$headerSize);
        $result = $body;
        curl_close($this->ch);
        return $result;
    }

    function getallheaders(){
        return $this->ret_headeres;
    }

}

?>